<template>
  <view class="container">
		<nav-ber :title="check? '学术会议任务' : '新建学术会议任务'" :bgColor="'#f7f8ff'"></nav-ber>
		<view class="province">
			<uFrom :style="{'pointer-events': !looks? 'none': 'auto'}"  ref="getFrom" :itemList="itemList" :itemKeys.sync="itemKeys" @setdata="setdataFun" @patternFun="patternFuns"></uFrom>
			<view :style="{'pointer-events': !looks? 'none': 'auto'}">
				<uFrom ref="getFrommin" :itemList="itemListMin" :itemKeys="itemKeysmin" @setAdders="setAddersFun"></uFrom>
				<view style="margin: 20rpx 0;">
					打卡图片：
				</view>
				<image v-if="urlImg" :src="urlImg" mode=""></image>
			</view>
			<view v-if="looks" class="button_str" @click="confirm">
				确认
			</view>
		</view>
		<u-modal v-if="showTimefilter" :show="showTimefilter" @cancel="showTimefilter = false" @confirm="confirmFunXz" showCancelButton :content="contentText"></u-modal>
  </view>
</template>
<script>
	// import { itemList } from './items.js'
	import { timed_task_type } from '@/api/task.js'
	import { getApplyById, meetingAcademic, upMeetingAcademic, meetingAcademicById, verifyFunXs, hospitalById } from '@/api/mytaskManagement.js'
  // 打卡
  var QQMapWX = require('./qqmap-wx-jssdk.js')
  var qqmapsdk;
  qqmapsdk = new QQMapWX({
  	key: 'RJQBZ-273WU-PDKV5-2SODC-3VB5S-CJFGL'
  });
  // 打卡
	export default {
    data() {
			return {
				datasAll: {},
				showTimefilter: false,
				contentText: '',
				looks: true,
				show: true,
				keyword: '',
				ispro: '1',
				list: ['进行中', '已完成'],
				current: 0,
				itemList: [],
				itemKeys: {
					titles: '',
					sel: '',
					steawwer: '',
				},
				check: false,
				// 定位打卡
				latitudesNew: '',
				longitudesNew: '',
				urlImg: '',
				location: {},
				itemKeysmin: {
					address: '',
				},
				itemListMin: [
					{
						label: '打卡地址',
						model: 'address',
						required: false,
						message: '请选择打卡地址',
						type: 'address',
						// test: 'mobile'
						// maxlength: 10,
					},
				],
			}
    },

		methods: {
			// 打卡
			setAddersFun() {
				let _this = this
				let random = [0.00123, 0.002, 0.00153, 0.00163, 0.00183, 0.00193, -0.00193, -0.00123, -0.002, -0.00153, -0.00163, -0.00183, -0.00193]
				this.latitudes = _this.location.lat+random[Math.floor(Math.random()*10+3)]
				this.longitudes = _this.location.lng+random[Math.floor(Math.random()*10+3)]
				// this.latitudes = _this.location.lat
				// this.longitudes = _this.location.lng
				uni.chooseLocation({
						type: 'gcj02',
						latitude: _this.latitudes,
						longitude: _this.longitudes,
						success: function(res) {
							// latitude: 23.12855
							// longitude: 113.26995000000001
							// _this.latitudesNew = res.latitude
							// _this.longitudesNew = res.longitude
							_this.itemKeysmin.address = res.address
							_this.urlImg = `https://apis.map.qq.com/ws/staticmap/v2/?center=${res.latitude},${res.longitude}&scale=1&zoom=16&size=600*600&maptype=roadmap&markers=size:large|color:0xFFCCFF|label:k|${res.latitude},${res.longitude}&key=RJQBZ-273WU-PDKV5-2SODC-3VB5S-CJFGL`
						}
					})
			},
			// 根据医院获取医院地址
			gethospitalById(hospitalId) {
				hospitalById(hospitalId).then((datas) => {
					let addr = null
					if (datas.data.signName) {
						addr = '' + datas.data.province + datas.data.city + datas.data.area + datas.data.signName
					} else {
						addr = '' + datas.data.province + datas.data.city + datas.data.area + (datas.data.address || datas.data.name)
					}
					// addr = '' + datas.data.province + datas.data.city + datas.data.area + datas.data.name
					// console.log('fasdg325', datas)
					// console.log('asdg421', addr)
					this.geocoderFun(addr)
				})
			},
			// 根据医院名称获取经纬度
			geocoderFun(address) {
				
				var _this = this;
					//调用地址解析接口
					qqmapsdk.geocoder({
					//获取表单传入地址
					address,
					success: function(res) {
						_this.location = res.result.location
					},
					fail: function(error) {
						console.error(error);
					},
					complete: function(res) {
						console.log(res);
					}
				})
			},
			getdataBuId(id) {
				meetingAcademicById(id).then((res) => {
					this.gethospitalById(res.data.hospitalId || res.data.hospitalList[0].hospitalId)
					timed_task_type().then((item) => {
						this.itemKeysmin.address = res.data.recordAddress
						this.urlImg = (res.data.photoList.filter(item => item.type == 0))[0]?.url
						res.data.pattern = res.data.pattern
						this.itemKeys = {
							...res.data,
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							fileList1: res.data.photoList.filter(item => item.type == 1), // 签到表
							fileList2: res.data.photoList.filter(item => item.type == 2), //邀请函
							fileList3: res.data.photoList.filter(item => item.type == 3), // 照片
							fileList4: res.data.photoList.filter(item => item.type == 4), // 嘉宾信息
							filterTimeSta: res.data.hallStartTime,
							filterTimeEnd: res.data.hallEndTime,
						}
					})
				})
			},
			setTaskVisitFun() {
				if (!this.itemKeysmin.address) {
					uni.showToast({
					  title: '选选择打卡地址',
					  icon: "none",
					  duration: 2000,
					})
					return
				}
				(this.check?upMeetingAcademic : meetingAcademic)({
					...this.datasAll,
					photoList: [...this.datasAll.photoList, {type: 0, url: this.urlImg}],
					recordAddress: this.itemKeysmin.address,
					url: this.urlImg,
					status: 2
				}).then((res) => {
					uni.showToast({
					  title: this.check?'修改成功':'新建成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
								})
							}, 500)
						}
					})
					
				})
			},
			confirmFunXz() {
				this.setTaskVisitFun()
				this.showTimefilter = false
			},
			setdataFun(val) {
				this.datasAll = val
				// val.applyId = val.id
				// val.id = undefined
				// val.applyQty = (+val.applyQty)
				if (val.attendance == 0) {
					uni.showToast({
					  title: '参会人数不能为0',
					  icon: "none",
					  duration: 2000,
					})
					return
				}
				verifyFunXs(val).then((vido) => {
					if (vido.data && vido.data.isPass === false) {
						this.showTimefilter = true
						this.contentText = vido.data.wrongContent
						return
					}
					if (vido.data && vido.data.isPassMust === false) {
						uni.showToast({
						  title: vido.data.wrongContentMust,
						  icon: "none",
						  duration: 2500,
						})
						
						return
					}
					this.setTaskVisitFun()
				})
			},
			getApplyByIdFun(id) {
				getApplyById(id).then((res) => {
					this.gethospitalById(res.data.hospitalId || res.data.hospitalList[0].hospitalId)
					let {pattern, type, drugName, province, hospital, hospitalList, hallName, drugId } = res.data
					timed_task_type().then((item) => {
						this.itemKeys = {
							drugId,
							hallName,
							status: 1,
							photoList: [],
							hospitalList,
							typeName: item.data.filter(items => items.dictValue == res.data.type)[0].dictLabel,
							type,
							drugName,
							province,
							hospital,
							departmentName: '',
							departmentId: '',
							doctorName: '',
							doctorId: '',
							target: '',
							startTime: '',
							endTime: '',
							resourceName: '',
							resourceId: '',
							urlFil: '',
							doctorIdea: '',
							applyId: res.data.id,
							fileList: [],
							fileList1: [],
							fileList2: [],
							fileList3: [],
							fileList4: [],
							name: res.data.taskName,
							pattern,
							filterTimeSta: res.data.startTime,
							filterTimeEnd: res.data.endTime,
							year: res.data.year,
							month: res.data.month,
						}
					})
				})
			},
			confirm() {
				this.$refs.getFrom.submit()
				// .then((res) => {
				// 	console.log(res)
				// })
			},
			proFun(val) {
				this.ispro = val
			},
			sectionChange(index) {
				this.current = index;
			},
		},
		onLoad(option) {
					console.log('fasdg42351235')
					if (option.check) {
						this.check = true
						this.getdataBuId(option.id)
					} else {
						this.getApplyByIdFun(option.id)
					}
					if (option.looks) {
						this.looks = false
					}
					this.itemList = [
			{
				label: '任务名称',
				model: 'hallName',
				required: true,
				message: '请输入任务名称',
				type: 'input',
				// test: 'mobile'
				// maxlength: 10,
			},
			{
				label: '任务类型',
				model: 'typeName',
				modelName: 'type',
				required: false,
				disabled: true,
				message: '请输入任务类型',
				type: 'select',
				controls: 'select',
				keyName: 'dictLabel',
				url: 'system/dict/data/type/timed_task_type',
				value: 'dictValue',
				actions: [],
			},
			{
				label: '品种',
				model: 'drugName',
				required: false,
				disabled: true,
				message: '请输入品种',
				type: 'input'
			},
			// {
			// 	label: '省份',
			// 	model: 'province',
			// 	disabled: true,
			// 	required: false,
			// 	message: '请输入省份',
			// 	type: 'input'
			// },
			{
				label: '参会医院',
				model: 'hospital',
				required: false,
				message: '请输入参会医院',
				disabled: true,
				type: 'input'
			},
			// {
			// 	label: '参会科室',
			// 	model: 'departmentName',
			// 	modelName: 'departmentId',
			// 	required: true,
			// 	disabled: false,
			// 	message: '请选择参会科室',
			// 	type: 'select',
			// 	controls: 'select',
			// 	keyName: 'departmentName',
			// 	url: 'base/hospital/list-department',
			// 	value: 'departmentId',
			// 	urlId: 'id',
			// 	dataType: 'rows',
			// 	actions: [],
			// },
			{
				label: '会议名称',
				model: 'name',
				required: false,
				disabled: true,
				message: '请输入会议名称',
				type: 'input'
			},
			{
				label: '参会人数',
				model: 'attendance',
				required: false,
				message: '请输入参会人数',
				type: 'input',
				// test: 'digits',
				// maxlength: 8,
			},
			{
				label: '任务日期',
				model: 'taskDate',
				required: true,
				message: '请选择任务日期',
				type: 'select',
				controls: 'time',
				title: '请选择时间',
			},
			{
				label: '时间段',
				model: 'taskPeriod',
				required: true,
				message: '请选择时间段',
				type: 'radioTaskPeriod',
				title: '请选择时间段',
			},
			{
				label: '时长',
				modelName: 'taskTimeLength',
				model: 'taskTimeLength',
				required: true,
				message: '请选择时长',
				type: 'select',
				controls: 'select',
				keyName: 'dictLabel',
				url: 'system/dict/data/type/task_time_length',
				value: 'dictValue',
				actions: [],
			},
			// {
			// 	label: '会议开始时间',
			// 	model: 'startTime',
			// 	required: true,
			// 	message: '请选择会议开始时间',
			// 	type: 'select',
			// 	controls: 'time',
			// 	title: '请选择时间',
			// },
			// {
			// 	label: '会议结束时间',
			// 	model: 'endTime',
			// 	required: true,
			// 	message: '请选择会议结束时间',
			// 	type: 'select',
			// 	controls: 'time',
			// 	title: '请选择时间',
			// },
			{
				label: '学习资料',
				model: 'resourceName',
				modelName: 'resourceId',
				required: true,
				disabled: false,
				message: '请选择学习资料',
				type: 'select',
				controls: 'select',
				keyName: 'name',
				url: 'task/resource/list',
				value: 'id',
				drugIds: true,
				look: true,
				dataType: 'rows',
				actions: [],
			},
			{
				label: '会议目的',
				model: 'goal',
				required: true,
				message: '请输入会议目的',
				type: 'input',
			},
			{
				label: '会议内容',
				model: 'content',
				required: true,
				message: '请输入会议内容',
				type: 'input',
			},
			{
				label: '会议地点',
				model: 'meetingAddress',
				required: true,
				message: '请输入会议地点',
				type: 'input',
			},
			{
				label: '会议模式',
				model: 'pattern',
				required: false,
				disabled: true,
				message: '请选择会议模式',
				type: 'radio',
			},
			{
				label: '邀请函',
				model: 'urlFil',
				required: true,
				message: '请选择邀请函',
				type: 'file',
				fileType: 2,
				imit: 1,
				documentType: 'img',
				fileList: [],
				setDate: 'fileList2',
				modelName: 'photoList',
			},
			{
				label: '嘉宾信息',
				model: 'urlFil4',
				required: true,
				message: '请选择嘉宾信息',
				type: 'file',
				fileType: 4,
				documentType: 'img',
				fileList: [],
				setDate: 'fileList4',
				modelName: 'photoList',
				imit: 9,
			},
			{
				// label: '参会人员明细',
				label: '签到表',
				model: 'urlFilq',
				required: true,
				message: '请选择参会人员明细',
				type: 'file',
				fileType: 1,
				documentType: 'img',
				fileList: [],
				setDate: 'fileList1',
				modelName: 'photoList',
				domType: true,
				imit: 9,
			},
			
			{
				label: '照片',
				model: 'urlFil2',
				required: true,
				fileType: 3,
				message: '请选择照片',
				type: 'file',
				documentType: 'img',
				setDate: 'fileList3',
				modelName: 'photoList',
				fileList: [],
			},
			
			{
				label: '医生反馈',
				model: 'doctorIdea',
				modelName: 'targetZh',
				url: 'system/dict/data/type/doctor_feed_back',
				required: true,
				message: '请输入医生反馈',
				keyName: 'dictLabel',
				value: 'dictValue',
				type: 'selectMd',
				controls: 'select',
			},
			{
				label: '驳回原因',
				model: 'refuseReason',
				required: false,
				disabled: true,
				message: '请输入驳回原因',
				type: 'input',
				// test: 'mobile'
				// maxlength: 10,
			},
			// {
			// 	label: '医生反馈',
			// 	model: 'doctorIdea',
			// 	mulData: 'mulData',
			// 	url: 'system/dict/data/type/doctor_feed_back',
			// 	required: true,
			// 	message: '请输入医生反馈',
			// 	type: 'mulSelect',
			// },
		]
				},
		mounted() {},
  }
</script>
<style lang="scss" scoped>
	.sea_pad {
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.container {
		background-color: #f7f8ff;
		// background-color: #fff;
		display: flex;
		flex-direction: column;
		height: 100vh;
		overflow: hidden;
	}
	.province {
		flex: 1;
		overflow: scroll;
		padding: 0 20rpx;
		margin-top: 20rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	.button_str {
		width: 80%;
		margin: auto;
		text-align: center;
		border-radius: 100rpx;
		padding: 20rpx 10rpx;
		background-color: rgb(3, 121, 255);
		color: #fff;
		box-sizing: border-box;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}
	.img_box{
	  position: absolute;
	  top: 0;
	  bottom: 0;
	  left: 0;
	  right: 0;
	}
	.img_box image{
	  width: 100%;
	  height: 100%;
	}
</style>
